package com.wphmoon.lesson.common.typehandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

//@MappedTypes(value = String.class)
//@MappedJdbcTypes(value=JdbcType.VARCHAR)
public class NullToEmptyStringTypeHandler implements TypeHandler<String> {

	@Override
	public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
		System.out.println("into NullToEmptyStringTypeHandler,parameter="+parameter+",jdbcType="+jdbcType);
		if(parameter == null && jdbcType == JdbcType.VARCHAR){//判断传入的参数值是否为null
              ps.setString(i,"");//设置当前参数的值为空字符串
          }else{
              ps.setString(i,parameter);//如果不为null，则直接设置参数的值为value
          }
		
	}

	@Override
	public String getResult(ResultSet rs, String columnName) throws SQLException {
		return rs.getString(columnName);
	}

	@Override
	public String getResult(ResultSet rs, int columnIndex) throws SQLException {
		return rs.getString(columnIndex);
	}

	@Override
	public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
		return cs.getString(columnIndex);
	}

 
}
